<?php
/**
 * Samble HTML
 * Samble html factory class file .
 *
 * @author Lim Afriyadi
 * @package Samble
 * @subpackage html
 * @version pre-alpha
 * @license GNU/GPL .
 */

 class HTML implements HTMLForms,HTMLElement,HTMLHead,HTMLPage,HTMLStyle
 {
    const SUBMIT_BTN = 'submit';
    const SUBMIT_IMG = 'image';
    const FORM_POST = 0;
    const FORM_GET = 1;
    const DTD_4_TRANSITIONAL = 1;
    const DTD_4_STRICT = 2;
    const DTD_4_FRAMESET = 3;
    const DTD_1_XTRANSITIONAL = 4;
    const DTD_1_XSTRICT = 5;
    const DTD_1_XFRAMESET = 6;
    /**
     * Array of stored Forms identified by their id attribute .
     * @var array $a_Form_Id
     */
    var $a_Form_Id = array(  );
    /**
     * Array of stored Tables identified by their id attribute .
     * @var array $a_Table_Id
     */
    var $a_Table_Id = array(  );
    /**
     * Array of stored Images identified by their id attribute .
     * @var array $a_Image_Id
     */
    var $a_Image_Id = array(  );
    /**
     * Array of Pre-defined HTML DTDs to be compared with user input .
     * @var array $a_DTD
     * @final
     */
    var $a_DTD =
        array(
            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4 . 01 Transitional//EN" "http://www . w3 . org/TR/html4/loose . dtd">',
            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4 . 01//EN" "http://www . w3 . org/TR/html4/strict . dtd">',
            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4 . 01 Frameset//EN" "http://www . w3 . org/TR/html4/frameset . dtd">',
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 . 0 Transitional//EN" "http://www . w3 . org/TR/xhtml1/DTD/xhtml1-transitional . dtd">',
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 . 0 Strict//EN" "http://www . w3 . org/TR/xhtml1/DTD/xhtml1-strict . dtd">',
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 . 0 Frameset//EN" "http://www . w3 . org/TR/xhtml1/DTD/xhtml1-frameset . dtd">'
         );
    /**
     * Prefix to create form tag <form> and register the created form
     * into form array identified by its 'id' attribute to be used again
     * later .
     *
     * @param string $s_Name Form name
     * @param stromg $s_Action Form action
     * @param string $s_Method Form method
     * @param string $s_Form_Id Form id (optional)
     * @param string $s_Class Form class (optional)
     * @param boolean $b_Multipart Multipart form for file
     * @return string | false .
     */
    public function getStartForm( $s_Name="",$s_Action="",$s_Method="",$s_Form_Id="",$s_Class="", $b_Multipart=false,$s_Param="" )
    {
        if( ( $s_Method == HTML::FORM_GET || $s_Method == HTML::FORM_POST ) && $s_Name !== "" )
        {
            $s_Form = "";
            if( $s_Method == HTML::FORM_POST ) $s_Method = "POST";
            if( $s_Method == HTML::FORM_GET ) $s_Method = "GET";
            $s_Form   .=  "\n" . '<form';
            $s_Form   .=  ' name="' . $s_Name . '"';
            $s_Form   .=  ' action="' . $s_Action . '"';
            $s_Form   .=  ' method="' . $s_Method . '"';
            if( $s_Form_Id !== "" ) $s_Form   .=  ' id="' . $s_Form_Id . '" ';
            if( $s_Class !== "" ) $s_Form   .=  ' class="' . $s_Class . '" ';
            if( $b_Multipart ) $s_Form .= ' enctype="multipart/form-data" ';
            $s_Form   .=  $s_Param . '>';
            $this->a_Form_Id[ $s_Form_Id ] = true;
            return $s_Form;
        }
        ErrorHandler::error( "Form method is not set . " );
        return false;
    }
    /**
     * Prefix to create form tag <form> and register the created form
     * into form array identified by its 'id' attribute to be used again
     * later .
     *
     * @param string $s_Name
     * @param stromg $s_Action
     * @param string $s_Method
     * @param string $s_Form_Id
     * @param stromg $s_Class
     * @return boolean
     */     
    public function startForm( $s_Name="",$s_Action="",$s_Method="",$s_Form_Id="",$s_Class="", $b_Multipart=false , $s_Param="" )
    {
        $s_Form = $this->getStartForm($s_Name, $s_Action, $s_Method, $s_Form_Id, $s_Class, $b_Multipart, $s_Param);
        if( $s_Form )
        {
            echo "\n" . $s_Form . "\n";
            return true;
        }
        return false;
    }
    /**
     * Create closing tag for form tag </form> based on their id attribute .
     *
     * @param string $s_Form_Id
     * @return string
     */
    public function getStopForm( $s_Form_Id="" )
    {
        if( array_search( $s_Form_Id, $this->a_Form_Id ) )
        {
            unset( $this->a_Form_Id[ $s_Form_Id ] );
            $this->a_Form_Id = array_values( $this->a_Form_Id );
        }
        return "</form>";
    }
    /**
     * Create closing tag for form tag </form> based on their id attribute .
     * 
     * @param string $s_Form_Id
     */
    public function stopForm( $s_Form_Id="" )
    {
        $s_Stop_From = $this->getStopForm( $s_Form_Id );
        echo "\n" . $s_Stop_From . "\n";
    }

    /**
     * Create a html textbox input $s_Name parameter are mandatory .
     * 
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getText( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" ) {
        if( $s_Name != "" )
        {
            $s_Input = '<input type="text" ';
            $s_Input   .=  'name ="' . $s_Name . '" ';
            if( $s_Value != "" )
            {
                $s_Input   .=  'value="' . $s_Value . '" ';
            }
            $s_Input   .=  'id="' . $s_Id . '" ';
            $s_Input   .=  'class="' . $s_Class . '" ';
            $s_Input   .=  $s_Param . ' />';
            return $s_Input;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }

    /**
     * Create a html textbox input $s_Name parameter are mandatory .
     *
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function text( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" ) {
        $s_Text = $this->getText( $s_Name, $s_Value, $s_Id, $s_Class, $s_Param );
        if( $s_Text )
        {
            echo $s_Text;
            return true;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a html password input $s_Name parameter are mandatory .
     *
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
     public function getPassword( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
     {
        if( $s_Name != "" )
        {
            $s_Password = '<input type="password" ';
            $s_Password   .=  'name="' . $s_Name . '" ';
            if( $s_Value != "" )
            {
                $s_Password   .=  'value="' . $s_Value . '" ';
            }
            $s_Password   .=  'id="' . $s_Id . '" ';
            $s_Password   .=  'class="' . $s_Class . '" ';
            $s_Password   .=  $s_Param . ' />';
            return $s_Password;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
     }
    /**
     * Create a html password input $s_Name parameter are mandatory .
     *
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function password( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" ) {
        $s_Password = $this->getPassword( $s_Name, $s_Value, $s_Id, $s_Class, $s_Param );
        if( $s_Password )
        {
            echo $s_Password;
            return true;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a html selectbox $s_Name and $a_Options parameter are mandatory .
     *
     * @param string $s_Name
     * @param array $a_Options
     * @param string $s_Selected
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getSelect( $s_Name="", $a_Options="", $s_Selected="", $s_Id="", $s_Class="", $s_Param="" ) {
        if( $s_Name != "" )
        {
            $s_Select    = "\n" . '<select ';
            $s_Select   .=  'name="' . $s_Name . '" ';
            $s_Select   .=  'id="' . $s_Id . '" ';
            $s_Select   .=  'class="' . $s_Class . '" ';
            $s_Select   .=  $s_Param . '>' . "\n";
            if( !$this->getOptions( $a_Options, $s_Selected ) )
            {
                return false;
            }
            $s_Select   .=  $this->getOptions( $a_Options, $s_Selected );
            $s_Select   .=  "\n" . '</select>';
            return $s_Select;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a html selectbox $s_Name and $a_Options parameter are mandatory .
     *
     * @param string $s_Name
     * @param array $a_Options
     * @param string $s_Selected
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function select( $s_Name="", $a_Options="", $s_Selected="", $s_Id="", $s_Class="", $s_Param="" ) {
        $s_Select = $this->getSelect( $s_Name, $a_Options, $s_Selected, $s_Id, $s_Class, $s_Param );
        if( $s_Select )
        {
            echo $s_Select;
            return true;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Parse an array to html options tag <option>
     *
     * @param array $a_Options
     * @param string $s_SelectedKey
     * @return false | array of options .
     */
    public function getOptions( $a_Options="", $s_SelectedKey="" ) {
        if( !is_array( $a_Options ) )
        {
            ErrorHandler::error( "Options are not valid!" );
            return false;
        }
        $s_Select = "";
        foreach( $a_Options as $s_Key=>$s_Value )
        {
            $s_Select   .=  '   <option value="' . $s_Key . '"';
            if( $s_Key == $s_SelectedKey )
            {
                $s_Select   .=  ' selected';
            }
            $s_Select   .=  '>' . $s_Value . '</option>' . "\n";
        }
        return $s_Select;
    }
    /**
     * Parse an array to html options tag <option>
     *
     * @param array $a_Options
     * @param string $s_SelectedKey
     * @return boolean .
     */
    public function options( $a_Options="", $s_Selected_Key="" ) {
        $a_Options = $this->options( $a_Options, $s_Selected_Key );
        if( $s_Select )
        {
            echo $s_Select;
            return true;
        }
        ErrorHandler::error( "options(  ) first parameter was invalid!" );
        return false;
    }
    /**
     * Create a html radiobox .  $s_Name and $s_Value parameter are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Selected
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getRadio( $s_Name="",$s_Value="",$s_Selected=false, $s_Id="", $s_Class="", $s_Param="" )
    {
        if( $s_Name != "" && $s_Value != "" )
        {
            $s_Radio = '<input type="radio" name="' . $s_Name . '" value="' . $s_Value . '" id="' . $s_Id . '" class="' . $s_Class . '" ' . $s_Param;
            if( $s_Selected )
            {
                $s_Radio   .=  'checked="checked"';
            }
            $s_Radio   .= ' />';
            return $s_Radio;
        }
        $s_Err_Msg = 'Radio button( id="' . $s_Id . '" ) is missing attribute "name" or "value"!';
        ErrorHandler::error( $s_Err_Msg );
        return false;
    }
    /**
     * Create a html radiobox .  $s_Name and $s_Value parameter are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Selected
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function radio( $s_Name="",$s_Value="",$s_Selected=false, $s_Id="", $s_Class="", $s_Param="" )
    {
        $s_Radio = $this->getRadio( $s_Name, $s_Value, $s_Selected, $s_Id, $s_Class, $s_Param );
        if( $s_Radio )
        {
            echo $s_Radio;
            return true;
        }
        $s_Err_Msg = 'Radio button( id="' . $s_Id . '" ) is missing attribute "name" or "value"!';
        ErrorHandler::error( $s_Err_Msg );
        return false;
    }
    /**
     * Create a html checkbox .  $s_Name and $s_Value parameter are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Selected
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getCheckbox( $s_Name="", $s_Value="", $b_Selected=false, $s_Id="", $s_Class="", $s_Param="" ) {
        if( $s_Name !== "" && $s_Value !== "" )
        {
            $s_Checkbox = '<input type="checkbox" name="' . $s_Name . '" value="' . $s_Value . '" id="' . $s_Id . '" class="' . $s_Class . '" ' . $s_Param;
            if( $b_Selected )
            {
                $s_Checkbox   .=  'checked="checked"';
            }
            $s_Checkbox   .= ' />';
            return $s_Checkbox;
        }
        $s_Err_Msg = 'Checkbox( id="' . $s_Id . '" ) is missing attribute "name" or "value"!';
        ErrorHandler::error( $s_Err_Msg );
        return false;
    }
    /**
     * Create a html checkbox .  $s_Name and $s_Value parameter are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Selected
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function checkbox( $s_Name="", $s_Value="", $b_Selected=false, $s_Id="", $s_Class="", $s_Param="" ) {
        $s_Checkbox = $this->getCheckbox( $s_Name, $s_Value, $b_Selected, $s_Id, $s_Class, $s_Param );
        if( $s_Checkbox )
        {
            echo $s_Checkbox;
            return true;
        }
        $s_Err_Msg = 'Checkbox( id="' . $s_Id . '" ) is missing attribute "name" or "value"!';
        ErrorHandler::error( $s_Err_Msg );
        return false;
    }
    /**
     * Create a html textarea .  $s_Name parameter is mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getTextarea( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        if( $s_Name != "" )
        {
            $s_Textarea = '<textarea name="' . $s_Name . '" ';
            $s_Textarea   .=  'id="' . $s_Id . '" ';
            $s_Textarea   .=  'class="' . $s_Class . '" ';
            $s_Textarea   .=  $s_Param .  '>';
            if( $s_Value!="" )
            {
                $s_Textarea   .=  $s_Value;
            }
            $s_Textarea   .=  '</textarea>';
            return $s_Textarea;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a html textarea .  $s_Name parameter is mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function textarea( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        $s_Textarea = $this->getTextarea( $s_Name, $s_Value, $s_Id, $s_Class, $s_Param );
        if( $s_Name != "" )
        {
            echo $s_Textarea;
            return true;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a file inputbox .  $s_Name parameter is mandatory .
     * @param string $s_Name
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getInputfile( $s_Name="", $s_Id="", $s_Class="", $s_Param="" )
    {
        if( $s_Name != "" )
        {
            $s_File = '<input type="file" name="' . $s_Name . '" id="' . $s_Id . '" class="' . $s_Class . '" ' . $s_Param . ' />';
            return $s_File;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a file inputbox .  $s_Name parameter is mandatory .
     * @param string $s_Name
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function inputfile( $s_Name="", $s_Id="", $s_Class="", $s_Param="" )
    {
        $s_Input_File = $this->getInputfile( $s_Name, $s_Id, $s_Class, $s_Param );
        if( $s_Input_File )
        {
            echo $s_Input_File;
            return true;
        }
        ErrorHandler::error( "Name attribute is not defined!" );
        return false;
    }
    /**
     * Create a html button .  $s_Name and $s_Value parameters are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return string | false
     */
    public function getButton( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        if( $s_Name != "" && $s_Value != "" )
        {
            $s_Button = '<input type="button" name="' . $s_Name . '" value="' . $s_Value . '" id="' . $s_Id . '" class="' . $s_Class . '" ' . $s_Param . ' />';
            return $s_Button;
        }
        ErrorHandler::error( "Button( '" . $s_Id . "' ) is missing 'name' or 'value' attribute!" );
        return false;
    }
    /**
     * Create a html button .  $s_Name and $s_Value parameters are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function button( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        $s_Button = $this->getButton( $s_Name, $s_Value, $s_Id, $s_Class, $s_Param );
        if( $s_Button )
        {
            echo $s_Button;
            return true;
        }
        ErrorHandler::error( "Button( '" . $s_Id . "' ) is missing 'name' or 'value' attribute!" );
        return false;
    }
    /**
     * Create a html submit button .  $s_Name and $s_Value parameters are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function getSubmit( $s_Type=self::SUBMIT_BTN, $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        if( $s_Name !== "" && $s_Value !== "" )
        {
            $s_Submit = '<input type="' . $s_Type . '" name="' . $s_Name . '" value="' . $s_Value . '" id="' . $s_Id . '" class="' . $s_Class . '" ' . $s_Param . ' />';
            return $s_Submit;
        }
        ErrorHandler::error( "Submit button( '" . $s_Id . "' ) is missing 'name' or 'value' attribute!" );
        return false;
    }
    /**
     * Create a html submit button .  $s_Name and $s_Value parameters are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function submit( $s_Type=self::SUBMIT_BTN, $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        $s_Submit = $this->getSubmit( $s_Type, $s_Name, $s_Value, $s_Id, $s_Class, $s_Param );
        if( $s_Submit )
        {
            echo $s_Submit;
            return true;
        }
        ErrorHandler::error( "Submit button( '" . $s_Id . "' ) is missing 'name' or 'value' attribute!" );
        return false;
    }
    /**
     * Create an opening tag for html table <table> along with the array of header
     * to be the table header if it was given in the parameters .  Tablle created by this
     * function is stored in Samble table of array identified by its 'id' .
     * $s_Table_Id parameter is mandatory .
     *
     * Note : Table header element is either string or two dimessional array identified by
     * string index 'attr' as html 'th' attributes string and 'caption' for 'th' value.
     *
     * Example 1 (table header single dimensional array) :
     * <code>
     * $html->startTable('test',array('No', 'Name'));
     * </code>
     * This will generate output like the followings
     * <code>
     * <table id="test"><thead><th>No</th><th>Name</th></thead><tbody>
     * </code>
     *
     * Example 2 (table header with two dimensional array) :
     * <code>
     * $html->startTable(
     *          'test',
     *          array(
     *            array(
     *               'attr'=>'colspan="2"',
     *               'caption'=>'No'
     *            ),
     *            'Name'
     *          )
     * )
     * </code>
     *
     * this will generate output like the followings :
     * <code>
     * <table id="test"><thead><th colspan="2">No</th><th>Name</th></thead><tbody>
     * </code>
     *
     * @param string $s_Table_Id
     * @param string $a_Table_Header
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function startTable( $s_Table_Id="", $a_Table_Header="", $s_Class="", $s_Param="" )
    {
        if( $s_Table_Id !="" )
        {
            $s_Table= "\n" . '<table id="' . $s_Table_Id . '" class="' . $s_Class . '" ' . $s_Param . ' >' . "\n";
            if( is_array( $a_Table_Header ) )
            {
                $s_Table   .=  "<thead><tr>";
                foreach( $a_Table_Header as $o_Header )
                {
                    if(is_array($o_Header) && $o_Header['attr'] && $o_Header['caption'])
                    {
                        $s_Table   .=  '<th ' . $o_Header['attr'] . '>' . $o_Header['caption'] . '</th>';
                    }
                    else if(!is_array($o_Header))
                    {
                        $s_Table   .=  ' <th>' . $o_Header . '</th>';
                    }
                    else
                    {
                        return false;
                    }
                }
                $s_Table   .= '</tr></thead><tbody>';
            }
            $this->s_Table = $s_Table;
            echo $s_Table;
            return true;
        }
        ErrorHandler::error( "Attibute id is not defined!" );
        return false;
    }
    /**
     * Create html table row tag <tr> along with the table data <td> and
     * print out the result .  Paramenters are mandatory .
     * @param string $sTableRow
     * @param array $a_Table_Column
     * @return boolean
     */
    public function tableRow( $a_Table_Column=array(  ),$s_Param="" )
    {
       if( is_array( $a_Table_Column ) )
        {
            $s_Row = "\n" . '<tr';
            if( $s_Param != "" )
            {
                $s_Row   .=  ' ' . $s_Param;
            }
            $s_Row   .=  '>';
            foreach( $a_Table_Column as $s_Column )
            {
                $s_Row   .=  $s_Column;
            }
            $s_Row   .=  "\n" . '</tr>';
            if( $this->s_Table )
            {
                $this->s_Table   .=  $s_Row;
            }
            echo $s_Row;
            return true;
        }
        ErrorHandler::error( "Table Row( '" . $this->table . "' ) is missing value attribute!" );
        return false;
    }
    /**
     * Parse array to html table data and print out the result .
     * @param array $a_Table_Column
     * @return false || array of html table data
     */
    public function tableColumn( $a_Table_Column=array() )
    {
        if( is_array( $a_Table_Column ) )
        {
            $s_Column = "";
            $i=0;
            foreach( $a_Table_Column as $col )
            {
                $s_Column[ $i ] = "\n" . '        <td';
                if( is_array($col) )
                {
                    $args = 1;
                    while( $args < count($col) )
                    {
                        $s_Column[ $i ]   .=  ' ' . $col[$args];
                        $args++;
                    }
                    $s_Column[ $i ]   .=  '>' . $col[0] . '</td>';
                }
                else
                {
                    $s_Column[ $i ]   .=  '>' . $col . '</td>';
                }
                $i++;
            }
            return $s_Column;
        }
        ErrorHandler::error( "Table Coloumns( '" . $this->table . "' ) is missing value attribute!" );
        return false;
   }
   /**
    * Create closing tag for an html table identified by its 'id' parameter .
    * And an option to keep the table into Samble table array or remove it .
    * All parameters are mandatory
    * @param string $s_Table_Id
    * @param string $bDestroy Option to remove or keep the table in Samble array of table .
    * @return boolean
    */
   public function stopTable( $s_Table_Id="",$b_Destroy=false )
   {
       // $s_Table_Id parameter is mandatory .
       if( $s_Table_Id != "" )
       {
           // Destory table action .
           if( $b_Destroy )
           {
               // Find the table id ins_Id $this->a_Table_Id .
               if( array_search( $s_Table_Id, $this->a_Table_Id ) )
               {
                   echo "\n" . '</table>' . "\n";
                   unset( $this->a_Table_Id[ $s_Table_Id ] );
                   $this->a_Table_Id= array_values( $this->a_Table_Id );
                   return true;
               }
               else
               {
                   ErrorHandler::error( 'Table( id="' . $s_Table_Id . '" ) is not found!' );
               }
           }
           else
           {
               echo "\n" . '</tbody></table>' . "\n";
               $this->a_Table_Id[ $s_Table_Id ] = $this->s_Table;
               return true;
           }
       }
       else
       {
            ErrorHandler::error( "Attribute 'id' is not defined!" );
       }
       return false;
   }
   /**
    * Retrive a table from Samble array of stored table identified by the table id .
    * Paramenters are mandatory .
    * @param string $s_Table_Id
    * @return boolean
    */
   public function getTable( $s_Table_Id )
   {
       if( $this->a_Table_Id[ $s_Table_Id ] )
       {
           return $this->a_Table_Id[ $s_Table_Id ];
       }
   }

   /**
    * Create html image tag <img> and store it in Samble image array identified by
    * 'id' parameter .  $s_Img_Src and $s_Img_Id paramenters are mandatory .
    * @param string $s_Img_Src
    * @param string $s_Img_Id
    * @param string $s_Class
    * @param string $s_Param
    * @return boolean
    */
   public function img( $s_Img_Src="",$s_Img_Id="", $s_Class="", $s_Param="" )
   {
       // Validate $s_Img_Id parameter .
       if( $s_Img_Id !="" )
       {
           // Validate $s_Img_Src parameter .
           if( $s_Img_Src !=""  )
           {
                $s_Img = '<img src="' . $s_Img_Src . '" id="' . $s_Img_Id . '" class="' . $s_Class . '" ' . $s_Param . ' />';
                echo $s_Img;
                $this->a_Image_Id[ $s_Img_Id ] = $s_Img;
                return true;
           }
           // For invalid $s_Img_Src
           ErrorHandler::error( "Image ( id='" . $s_Img_Id . "' ) source is not defined!" );
       }
       // For invalid $s_Img_Id .
       else
       {
           ErrorHandler::error( "Image 'id' attribute is not defined!" );
       }
       return false;
   }

   /**
    * Get an image from Samble array of image identified by its 'id' parameter .
    * All parameters are mandatory .
    * @param string $s_Img_Id
    * @return boolean
    */
   public function getImage( $s_Img_Id="" )
   {
       if( $this->a_Image_Id[ $s_Img_Id ] )
       {
           echo $this->a_Image_Id[ $s_Img_Id ];
           return true;
       }
       ErrorHandler::error( 'Image ( id="' . $s_Img_Id . '" ) is not found!' );
       return false;
   }

   /**
    * Create an html iframe tag <iframe> .  $s_Frame_Id and $s_Frame_Src are mandatory .
    * @param string $s_Frame_Src
    * @param string $s_Frame_Id
    * @param string $s_Param
    * @return boolean
    */
   public function iframe( $s_Frame_Src="", $s_Frame_Id="",$s_Param="" )
   {
       // Validate $s_Frame_Id parameter .
       if( $s_Frame_Id != "" )
       {
           // Validate $s_Frame_Src parameter .
           if( $s_Frame_Src !="" )
           {
                $s_Frame = '<iframe src="' . $s_Frame_Src . '" id="' . $s_Frame_Id . '" ' . $s_Param . ' />';
                echo $s_Frame;
                return true;
           }
           // For invalid $s_Frame_Src .
           ErrorHandler::error( "Iframe ( id='" . $s_Frame_Id . "' ) 'src' attribute is not defined!" );
       }
       // For invalid $s_Frame_Id .
       else
       {
           ErrorHandler::error( "Iframe 'id' attribute is not defined!" );
       }
       return false;
   }
   /**
    * Make html anchor tag <a> .  $s_Link_Id, $s_Href, and $s_Value parameters are mandatory .
    * @param string $s_Href
    * @param string $s_Value
    * @param string $s_Link_Id
    * @param string $s_Class
    * @param string $s_Param
    * @return boolean
    */
   public function startLink( $s_Href="",$s_Value="",$s_Link_Id="", $s_Class="", $s_Param="" )
   {
       // Validate $s_Link_Id parameter .
       if( $s_Link_Id != "" )
       {
           // Validate $s_Href parameter .
           if( $s_Href !="" )
           {
              $s_Link= "\n" . '<a href="' . $s_Href . '" id="' . $s_Link_Id . '" class="' . $s_Class . '" ' . $s_Param . ' >' . $s_Value;
              echo $s_Link;
              return true;
           }
           // For invalid $s_Href .
           ErrorHandler::error( "Link ( id='" . $s_Link_Id . "' ) 'href' attribute is not defined!" );
       }
       // For invalid $s_Link_Id .
       else
       {
           ErrorHandler::error( "Link 'id' attribute is not defined!" );
       }
       return false;
   }

  /**
   * Close html anchor tag </a> identified by its 'id' .  $s_Link_Id parameter is mandatory
   * @param string $s_Link_Id
   * @return boolean
   */
  public function stopLink( $s_Link_Id="" )
  {
        if( $s_Link_Id !="" )
        {
            echo "</a>";
            return true;
        }
        ErrorHandler::error( 'Link ( id="' . $s_Link_Id . '" ) is not found! or Address is null' );
        return false;
  }

   /**
    * Create html container tag <div> . $s_Div_Id parameter is mandatory .
    * @param string $s_Div_Id
    * @param string $s_Class
    * @param string $s_Param
    * @return boolean
    */
   public function startDiv( $s_Div_Id="", $s_Class="", $s_Param="" ) {
        if ( $s_Div_Id !="" )
        {
            $s_Div = "\n" . '<div id="' . $s_Div_Id.'"';
            if($s_Class != "") $s_Div .= '" class="' . $s_Class . '" ';
            if($s_Param != "") $s_Div .= $s_Param;
            $s_Div .='>' . "\n";
            echo $s_Div;
            return true;
        }

        ErrorHandler::error( 'Div ( id="' . $s_Div_Id . '" ) or ( class="' . $s_Class . '"  )   is not found!' );
        return false;

   }
   /**
    * Close html container tag </div> identified by its id attribute .
    * $s_Div_Id parameter is mandatory
    * @param string $s_Div_Id
    * @return boolean
    */
   public function stopDiv( $s_Div_Id = "" ) {

       if ( $s_Div_Id !="" )
       {
           echo "\n" . '</div>' . "\n";
           return true;
       }
       ErrorHandler::error( 'Div ( id="' . $s_Div_Id . '" ) is not found!' );
       return false;
   }
   /**
    * Create html title tag <title> .  All parameters are mandatory .
    * @param string $s_Value
    * @return boolean
    */
   public function title( $s_Value="" )
   {
       if( $s_Value != "" )
       {
           echo "\n" . '<title>' . $s_Value . '</title>';
           return true;
       }
       ErrorHandler::error( "Title ( '<title>' ) is empty!" );
       return false;
   }
   /**
    * Create html head meta tag <meta/> .  $s_Param parameter is mandatory .
    * @param string $s_Param
    * @return boolean
    */
   public function meta( $s_Param="" )
   {
       if( $s_Param != "" )
       {
            echo "\n" . '<meta ' . $s_Param . ' />';
            return true;
       }
       ErrorHandler::error( "Meta ( '<meta>' ) parameters are invalid!" );
       return false;
   }
   /**
    * Create html head link tag <link/> .  $s_Param parameter is mandatory .
    * @param string $s_Param
    * @return boolean
    */
   public function link( $s_Param="" )
   {
       if( $s_Param != "" )
       {
            echo "\n" . '<link ' . $s_Param . ' />';
            return true;
       }
       ErrorHandler::error( "Link ( '<link>' ) parameters are invalid!" );
       return false;
   }
   /**
    * Create document DTD .  $s_Param parameter is mandatory .
    * @param const $s_Param HTML DTD constant of this class or user defined DTD .
    * @return boolean
    */
   public function dtd( $s_Param="" )
   {
       if( $s_Param != "" )
       {
            switch( $s_Param )
            {
                case self::DTD_4_TRANSITIONAL:
                    echo $this->a_DTD[ 0 ] . "\n";
                    break;
                case self::DTD_4_STRICT:
                    echo $this->a_DTD[ 1 ] . "\n";
                    break;
                case self::DTD_4_FRAMESET:
                    echo $this->a_DTD[ 2 ] . "\n";
                    break;
                case self::DTD_1_XTRANSITIONAL:
                    echo $this->a_DTD[ 3 ] . "\n";
                    break;
                case self::DTD_1_XSTRICT:
                    echo $this->a_DTD[ 4 ] . "\n";
                    break;
                case self::DTD_1_XFRAMESET:
                    echo $this->a_DTD[ 5 ] . "\n";
                    break;
                default:
                    if( substr( $s_Param, 0, 9 ) === "<!DOCTYPE" && substr( $s_Param,strlen( $s_Param )-1 ) === '>' ) echo $s_Param;
                    ErrorHandler::error( "Unknown DTD!" );
                    break;
            }
            return true;
       }
       ErrorHandler::error( "HTML DTD is not valid!" );
       return false;
   }
   /**
    * Create opening html body tag <body> .  Parameter is optional .
    * @param string $s_Param
    */
   public function startBody( $s_Param="" )
   {
       echo "\n" . '</head>' . "\n" . '<body';
       if( $s_Param != "" )
       {
            echo ' ' . $s_Param . ' ';
       }
       echo '>';
   }
   /**
    * Create opening html tag <html> .  Parameter is optional .
    * @param string $s_Param
    */
   public function startHTML( $s_Param="" )
   {
       echo '<html';
       if( $s_Param != "" )
       {
           echo ' ' . $s_Param . ' ';
       }
       echo '>' . "\n" . '<head>';
   }
   /**
    * Create closing html and body tag </body></html>
    */
   public function stopHTML()
   {
       echo "\n" . '</body>' . "\n" . '</html>';
   }
   /**
    * Create html style tag <style> . Parameters are mandatory .
    * @param string $s_Style
    * @return boolean
    */
   public function createStyle( $s_Style="" )
   {
       if( $s_Style != "" )
       {
           echo '<style>';
           echo $s_Style;
           echo '</style>';
           return true;
       }
       return false;
   }
   /**
    * Create html unordered list tag <ul> .  $s_Id parameter is mandatory .
    * @param string $s_Id
    * @param string $s_Class
    * @return boolean
    */
   public function startUl( $s_Id="" , $s_Class=""  )
   {
        if(  $s_Id !=""  )
        {
            $s_Ul ='<ul id="' . $s_Id .'"';
            if($s_Class != "") $s_Ul .= ' class="' . $s_Class . '"';
            $s_Ul .= '>';
            echo $s_Ul;
            return true;
        }
        ErrorHandler::error( "<ul> id attribute is not defined!" );
        return false;
   }
   /**
    * Create html unordered close tag </ul> .
    */
   public function stopUl()
   {
        echo "</ul>";
   }
   
    /**
     * Create html list open tag <li> .  $s_Id parameter is mandatory .
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function startLi( $s_Id="",$s_Class="",$s_Param="" )
    {
        if( $s_Id != "" )
        {
            $s_Li='<li id="' . $s_Id . '" class="' . $s_Class . '">';
            echo $s_Li;
            return true;
        }
        ErrorHandler::error( "<li> id attribute is not defined!" );
        return false;
    }
    /**
     * Create html list close tag </li> .
     */
    public function stopLi()
    {
        echo "</li>";
    }
    /**
     * Create html hidden input tag .  Name, value and id parameters are mandatory .
     * @param string $s_Name
     * @param string $s_Value
     * @param string $s_Id
     * @param string $s_Class
     * @param string $s_Param
     * @return boolean
     */
    public function getHidden( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        if( $s_Name != "" && $s_Value !== "" )
        {
            $s_Hidden = "\n";
            $s_Hidden .=  "<input type=\"hidden\" name=\"$s_Name\" value=\"$s_Value\"";
            if($s_Id !== "")
            {
                $s_Hidden  .=  " id=\"$s_Id\"";
            }
            if( $s_Class != "" )
            {
                $s_Hidden   .=  " class=\"$s_Class\"";
            }
            if( $s_Param != "" )
            {
                $s_Hidden   .=  ' ' . $s_Param;
            }
            $s_Hidden  .=  ' />';
            return $s_Hidden;
        }
        ErrorHandler::error( "Name, value and id parameters are mandatory!" );
        return false;
    }
    /**
     * 
     */
    public function hidden( $s_Name="", $s_Value="", $s_Id="", $s_Class="", $s_Param="" )
    {
        $s_Hidden = $this->getHidden($s_Name, $s_Value, $s_Id, $s_Class, $s_Param);
        if($s_Hidden)
        {
            echo $s_Hidden;
            return true;
        }
        return false;
    }
    /**
     *
     */
    public function getFile( $s_Name="", $s_Accept="", $s_Id="", $s_Class="" )
    {
        if(  $s_Name != "" )
        {
            $s_File = "\n";
            $s_File   .=  "<input type=\"file\" name=\"$s_Name\"";
            if( $s_Accept !== "" )
            {
                $s_File  .= "accept=\"$s_Accept\"";
            }
            if( $s_Class !== "")
            {
                $s_File   .=  " class=\"$s_Class\"";
            }
            if( $s_Id != "" )
            {
                $s_File   .=  " id=\"$s_Id\"";
            }
            $s_File  .=  ' />';
            return $s_File;
        }
        ErrorHandler::error( "Name, value and id parameters are mandatory!" );
        return false;
    }
    /**
     *
     */
    public function file( $s_Name="", $s_Accept="", $s_Id="", $s_Class="" )
    {
        $s_File = $this->getFile($s_Name, $s_Accept, $s_Id, $s_Class);
        if($s_File)
        {
            echo $s_File;
            return true;
        }
        return false;
    }
 }
?>